//+------------------------------------------------------------------+
//|                        MAB_TD_Sequential                         |
//|                              v1.2                                |
//|                                                                  |
//|   Indicator based off of Tom DeMarks' TD Sequential indicator as |
//|   described in Jason Perl's book "DeMark Indicators"             |
//|                                                                  |
//|   05/22/2009 - Created Buy and Sell Setup along with TDST lines  |
//|               and set up criteria for arrow and alert to appear  |
//|               for a perfect setup.                               |
//|      issues:  TDST line starts updating before a full setup is   |
//|               made                                               |
//|      still needs:  Countdown criteria                            |
//|                                                                  |
//|   05/26/2009 - Added the Countdown criteria.  Countdown will also|
//|               create a "+" when 13 is reach but not perfected.   |
//|      still needs:  Countdown recycle criteria(currently recycles |
//|                  automatically when setup completed)             |   
//+------------------------------------------------------------------+
#property copyright "Copyright ?2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Red
#property indicator_color2 Green
#property indicator_color3 MediumSpringGreen
#property indicator_color4 MediumSpringGreen
#property indicator_color5 Gold
#property indicator_color6 Gold

extern color Setup=MediumSpringGreen;
extern color buyCountdownColor=Gold;
extern color sellCountdownColor=Purple;

extern int NumBars=1000;
extern int Space=50;    //space above or below high to place number

extern bool Alerts=false;

double Support[], Resistance[], bPerfected[], sPerfected[], Buy[], Sell[];

datetime last_alert = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   //TDST lines
   SetIndexBuffer(0,Support);
   SetIndexStyle(0,DRAW_LINE, STYLE_DOT, 1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexBuffer(1,Resistance);
   SetIndexStyle(1,DRAW_LINE, STYLE_DOT, 1);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   
   //Perfected Setup
   SetIndexBuffer(2,bPerfected);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,225);
   SetIndexBuffer(3,sPerfected);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,226);
   
   //Countdown
   SetIndexBuffer(4,Buy);
   SetIndexStyle(4,DRAW_ARROW);
   SetIndexArrow(4,225);
   SetIndexBuffer(5,Sell);
   SetIndexStyle(5,DRAW_ARROW);
   SetIndexArrow(5,226);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----
    removeObjects();
//---- 
  

   return(0);
}

void removeObjects()
{
    for(int i=NumBars; i>=0; i--)
    {
        ObjectDelete(""+i);
        ObjectDelete("cd"+i);
        ObjectDelete("nine"+i);  
    }
    
    for (int j = ObjectsTotal() - 1; j >= 0; j--)
    {
        if (StringFind(ObjectName(j), "BuyCd") > 0 || StringFind(ObjectName(j), "SellCd") > 0)
        {
          ObjectDelete(ObjectName(j));
        }
    }
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   return;
   //int counted_bars=IndicatorCounted();
   int bSetup, sSetup, bCountdown, sCountdown;

   double tfm=Space*Point;
   double CountTest;
   bool bSetupInd, sSetupInd, bPerfect, sPerfect;

   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   removeObjects();
//----
   for(int i=NumBars; i>=0; i--)
      {
//+------------------------------------------------------------------+
//| Buy Setup                                                        |
//+------------------------------------------------------------------+
      Support[i]=Support[i+1];
      
      if(Close[i]<=Close[i+4] && Close[i+1]>=Close[i+5] && bSetup==0)//start setup
         {  
         bSetup++;
         ObjectCreate(""+i,OBJ_TEXT,0,Time[i],Low[i]-tfm);
         ObjectSetText(""+i,""+bSetup,8,"Arial",Setup);
         }
      if(Close[i]<Close[i+4] && bSetup!=0 && ObjectFind(""+i)==-1 && ObjectFind("nine"+i)==-1) 
         {
            bSetup++;
            if(bSetup==9)
            {
                ObjectCreate("nine"+i,OBJ_TEXT,0,Time[i],Low[i]-tfm);
                ObjectSetText("nine"+i,""+bSetup,10,"Arial Black",Setup); 
                bSetup=0; 
                bSetupInd=true;
                sSetupInd=false;
                
                Support[i+9]=EMPTY_VALUE;               
                for (int k= 0; k<=8; k++)
                {
                    Support[i+k]=High[i+8];   
                    Resistance[i+k]=EMPTY_VALUE;             
                }       
                
                int curI = i;
                while (sCountdown > 1)
                {
                    if (ObjectFind("cd"+curI) > -1)
                    {
                       if (ObjectDescription("cd"+curI) != "+")
                       {
                           sCountdown--;
                       }
                       ObjectDelete("cd"+curI);
                    }
                    curI++;
                }   
                sCountdown=0;  
            }
            else 
            {
                ObjectCreate(""+i,OBJ_TEXT,0,Time[i],Low[i]-tfm);
                ObjectSetText(""+i,""+bSetup,8,"Arial",Setup); 
            } 
         }
      //if setup is completed look for criteria for perfect setup
      if(bSetupInd==true)
         {
         if(Low[i+1]<=Low[i+3] || Low[i]<=Low[i+2])
            {
            bSetupInd=false;
            bPerfect=true;
            bPerfected[i]=Low[i]-tfm*2;
            bCountdown=1;
            }
          if(Alerts==True && last_alert!=Time[0])
            {
            last_alert = Time[0];
            Alert("TD Sequential - ", Symbol(), " Buy Signal Perfected");
            }  
          }         
      //Deletes numbers that were created if there is a break in sequence before 9 is reached
      else if(Close[i]>=Close[i+4] && bSetup!=0) 
         {
         for(int x=i+1; x<=i+bSetup+1; x++) ObjectDelete(""+x);
         bSetup=0; 
         }

//+------------------------------------------------------------------+                                         |
//+------------------------------------------------------------------+           
      
       if (High[i] > Support[i])
       {
         int curSI = i;
         /*
         while (bCountdown > 1)
         {
             if (ObjectFind("cd"+curSI) > -1)
             {                
                if (ObjectDescription("cd"+curSI) != "+")
                {
                    bCountdown--;
                }
                ObjectDelete("cd"+curSI);
             }
             curSI++;
         }
         */
         bCountdown = 0;   
       }   
//+------------------------------------------------------------------+
//| Buy Countdown Setup                                              |
//+------------------------------------------------------------------+
      if(bCountdown==13 && Low[i]<=Low[i+1] && Low[i]>CountTest)
         {
         //ObjectCreate("cd"+i,OBJ_TEXT,0,Time[i],Low[i]-tfm*2.5);
         //ObjectSetText("cd"+i,"+",8,"Arial",buyCountdownColor); 
         }
      if(bCountdown==13 && Low[i]<=Low[i+1] && Low[i]<=CountTest)
         {
         //ObjectCreate("cd"+i,OBJ_TEXT,0,Time[i],Low[i]-tfm*2.5);
         //ObjectSetText("cd"+i,""+bCountdown,8,"Arial",buyCountdownColor);
         bCountdown=0;
         if(Alerts==True && last_alert!=Time[0])
            {
            last_alert = Time[0];
            Alert("TD Sequential - ", Symbol(), " Buy Countdown Completed");
            }
         Buy[i]=Low[i]-tfm*4;
         }
     // if(bCountdown>=1 && bCountdown<13 && Low[i]<=Low[i+2])
         {
        // if(bCountdown==8)CountTest=Low[i];
         //ObjectCreate("cd"+i,OBJ_TEXT,0,Time[i],Low[i]-tfm*2.5);
         //ObjectSetText("cd"+i,""+bCountdown,8,"Arial",buyCountdownColor); 
         //bCountdown++;
         }  
        
         //ObjectFind()
         
         
        if(bCountdown ==1 && Low[i]<=Low[i+2])
        {
            ObjectCreate(i + "-BuyCd-"+bCountdown,OBJ_TEXT,0,Time[i],Low[i]-tfm*2.5);
            ObjectSetText(i + "-BuyCd-"+bCountdown,"1",8,"Arial",buyCountdownColor); 
            bCountdown++;
        }
          
        if (bCountdown > 1)
        {
            if (Low[i]<=Low[i+2] || Low[i]<=Low[i+1])
            {
                k = i+1;
                while (k <= NumBars && (Resistance[k] == EMPTY_VALUE || Resistance[k] == 0))
                {
                    if (ObjectFind(k + "-BuyCd-") > -1)
                    {
                        Print("K:",TimeToStr(Time[k]), " i:",TimeToStr(Time[i]));
                    }
                    k++;
                }
        
                //if (i != k)
                //Print("K:",TimeToStr(Time[k]), " i:",TimeToStr(Time[i]));
            }
        }
//+------------------------------------------------------------------+
//| Sell Setup                                                       |
//+------------------------------------------------------------------+
      Resistance[i]=Resistance[i+1];
      
      if(Close[i]>=Close[i+4] && Close[i+1]<=Close[i+5] && bSetup==0)//start setup
         {  
         sSetup++;
         ObjectCreate(""+i,OBJ_TEXT,0,Time[i],High[i]+tfm);
         ObjectSetText(""+i,""+sSetup,8,"Arial",Setup);
         }
      if(Close[i]>=Close[i+4] && sSetup!=0 && ObjectFind(""+i)==-1 && ObjectFind("nine"+i)==-1) 
         {
            sSetup++;
            if(sSetup==9) 
            {
                ObjectCreate("nine"+i,OBJ_TEXT,0,Time[i],High[i]+tfm);
                ObjectSetText("nine"+i,""+sSetup,10,"Arial Black",Setup); 
                sSetup=0; 
                sSetupInd=true;
                bSetupInd=false;
                
                curSI = i;
                
                /*
                while (bCountdown > 1)
                {
                    if (ObjectFind("cd"+curSI) > -1)
                    { 
                       if (ObjectDescription("cd"+curSI) != "+")
                       {
                           bCountdown--;
                       }
                       ObjectDelete("cd"+curSI);
                    }
                    curSI++;
                }
                */
                bCountdown = 0;       
                
                Resistance[i+9]=EMPTY_VALUE;      
                for (k= 0; k<=8; k++)
                {
                    Resistance[i+k]=Low[i+8];   
                    Support[i+k]=EMPTY_VALUE;               
                }  
            }
            else 
            {
                ObjectCreate(""+i,OBJ_TEXT,0,Time[i],High[i]+tfm);
                ObjectSetText(""+i,""+sSetup,8,"Arial",Setup); 
            } 
         }
      //Perfected Setup
      if(sSetupInd==true)
         {
         if(Low[i+1]>=Low[i+3] || Low[i]>=Low[i+2])
            {
            sSetupInd=false;
            sPerfect=true;
            sPerfected[i]=High[i]+tfm*2;
            sCountdown=1;
            }
         if(Alerts==True && last_alert!=Time[0])
            {
            last_alert = Time[0];
            Alert("TD Sequential - ", Symbol(), " Sell Signal Perfected");
            }
          }
      //Deletes numbers that were created if there is a break in sequence before 9 is reached
      else if(Close[i]<=Close[i+4] && sSetup!=0) 
         {
         for(x=i+1; x<=i+sSetup; x++) ObjectDelete(""+x);
         sSetup=0; 
         }

//+------------------------------------------------------------------+                                         |
//+------------------------------------------------------------------+  
      
      if (Low[i] < Resistance[i])
      {
         curI = i;
         while (sCountdown > 1)
         {
             if (ObjectFind("cd"+curI) > -1)
             {
                if (ObjectDescription("cd"+curI) != "+")
                {
                    sCountdown--;
                }
                ObjectDelete("cd"+curI);
             }
             curI++;
         }   
         
         sCountdown = 0;      
      }

//+------------------------------------------------------------------+
//| Sell Countdown Setup                                             |
//+------------------------------------------------------------------+      
      if(sCountdown==13 && High[i]>=High[i+2] && High[i]>=CountTest)
         {
         ObjectCreate("cd"+i,OBJ_TEXT,0,Time[i],High[i]+tfm*2.5);
         ObjectSetText("cd"+i,""+sCountdown,8,"Arial",sellCountdownColor);
         sCountdown=0;
         Sell[i]=High[i]+tfm*4;
         if(Alerts==True && last_alert!=Time[0])
            {
            last_alert = Time[0];
            Alert("TD Sequential - ", Symbol(), " Sell Countdown Completed");
            }
         } 
      if(sCountdown==13 && High[i]>=High[i+2] && High[i]<CountTest)
         {
         ObjectCreate("cd"+i,OBJ_TEXT,0,Time[i],High[i]+tfm*2.5);
         ObjectSetText("cd"+i,"+",8,"Arial",sellCountdownColor); 
         }
      if(sCountdown>=1 && sCountdown<13 && High[i]>=High[i+2])
         {
         if(sCountdown==8)CountTest=High[i];
         ObjectCreate("cd"+i,OBJ_TEXT,0,Time[i],High[i]+tfm*2.5);
         ObjectSetText("cd"+i,""+sCountdown,8,"Arial",sellCountdownColor); 
         sCountdown++;
         }
      }

      return(0);        
  }
//+------------------------------------------------------------------+